<template>
    <div style="margin: 10px">
        <h2>小熊分苹果</h2>
        <el-row :gutter="20">
            <el-col :span="12">
                <img src="./小熊分苹果.jpg">
            </el-col>
            <el-col :span="12">
                <el-input v-model="n"></el-input>
                <el-input disabled v-model="m"></el-input>
                <el-button @click="compute()">计算</el-button>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
        name: 'app',
        data() {
            return {
                n: 2,
                m: 0
            }
        },
        methods: {
            core(n) {
                let m = 0;
                let last = 0;
                //最大计算次数
                let count = 0;
                let max = 100000000;
                while (count < max) {
                    // x-(x-1)/n -1 = last
                    // x = (last*n +n - 1)/(n-1); 化简
                    // x = n * last/(n-1) +1
                    // 要使得每次都能整数分配，last 必须要是能够被(n-1)整除的整数,即x是整数
                    // 如此执行n步
                    let i = 1;
                    last = m;
                    for (i = 0; i < n; i++) {
                        let x = n * last / (n - 1) + 1;
                        if (!Number.isInteger(x)) {
                            break;
                        }
                        last = x;
                    }
                    if (i === n) {
                        break;
                    } else {
                        m++;
                    }
                    count++;
                }
                return last;
            },
            compute() {
                this.m = this.core(Number.parseInt(this.n));
            }
        },
        mounted() {

        }
    }
</script>

<style>
    h1, h2 {
        font-weight: normal;
    }

    ul {
        list-style-type: none;
        padding: 0;
    }

    li {
        display: inline-block;
        margin: 0 10px;
    }

    a {
        color: #42b983;
    }
</style>
